package com.zzz.gp.task;

import com.zzz.gp.dao.GpMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/12/20 16:58
 */
@Slf4j
@Component
public class CleanTack {
    @Autowired
    private GpMapper gpMapper;

    @Value("${clean.day}")
    private int day;

    @Scheduled(cron = "${clean.history}")
    public void clean(){
        LocalDate cleanDay=LocalDate.now().minusDays(day);
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime s = LocalDateTime.now();
        String day=dtf.format(cleanDay);
        int n=gpMapper.deleteHistoryData(day);

        LocalDateTime e = LocalDateTime.now();
        long seconds = Duration.between(s, e).getSeconds();
        log.info("清理表 get_history 历史数据 用时{}秒,删除{}条", seconds,n);

    }
}
